package com.yzy.domain.strategy.model.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.yzy.types.common.Constants;
import com.yzy.types.common.Constants.RuleKey;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author yangzhenyu05 <yangzhenyu05@kuaishou.com>
 * Created on 2024-01-12
 * @description 策略规则实体类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class StrategyRuleEntity {
    /**
     * 抽奖策略ID
     */
    private Long strategyId;
    /**
     * 抽奖奖品ID【规则类型为策略，则不需要奖品ID】
     */
    private Long awardId;
    /**
     * 抽象规则类型；1-策略规则、2-奖品规则
     */
    private Integer ruleType;
    /**
     * 抽奖规则类型【rule_random - 随机值计算、rule_lock - 抽奖几次后解锁、rule_luck_award - 幸运奖(兜底奖品)】
     */
    private String ruleModel;
    /**
     * 抽奖规则比值
     */
    private String ruleValue;
    /**
     * 抽奖规则描述
     */
    private String ruleDesc;

    public Map<String, List<Integer>> getRuleWeightValues() {
        if (!RuleKey.RULE_WEIGHT_KEY.equals(ruleModel)) {
            return null;
        }
        HashMap<String, List<Integer>> ruleWeightValueMap = new HashMap<>();
        String[] ruleValueGroups = this.ruleValue.split(Constants.SPLIT);
        for (String ruleValueGroup : ruleValueGroups) {
            // 检查输入是否为空
            if (Objects.isNull(ruleValueGroup) || StringUtils.isEmpty(ruleValueGroup)) {
                return ruleWeightValueMap;
            }
            // 分割字符串以获取键和值
            String[] parts = ruleValueGroup.split(Constants.COLON);
            if (parts.length != 2) {
                throw new IllegalArgumentException("rule_weight rule_rule invalid input format" + ruleValueGroup);
            }
            String[] valueStrings = parts[1].split(Constants.SPLIT);
            ArrayList<Integer> values = new ArrayList<>();
            for (String valueString : valueStrings) {
                values.add(Integer.parseInt(valueString));
            }
            ruleWeightValueMap.put(ruleValueGroup, values);
        }
        return ruleWeightValueMap;
    }
}
